<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object.defineProperties</title>
</head>
<body>
    <script>
        //要求添加 total 属性, 获得班级的总伤害
        var team = {
            name: 'RNG',
            damages: [
                {
                    name: 'Bin',
                    damage: 14630
                },
                {
                    name: 'Wei',
                    damage: 9894
                },
                {
                    name: 'XiaoHu',
                    damage: 20202
                },
                {
                    name: 'Gala',
                    damage: 14819
                },
                {
                    name: 'Ming',
                    damage: 4397
                }
            ]
        };
        
        //添加一个新属性
        Object.defineProperty(team, 'total', {
            get: function(){
                //声明一个变量
                var sum = 0;
                //循环
                this.damages.forEach(function(item){
                    sum += item.damage;
                });
                //返回
                return sum;
            }
        });

        //向 damages 数组中加入一个成员数据
        team.damages.push({
            name: '教练',
            damage: 2000
        });

        console.log(team.total);
    </script>
</body>

</html>